<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <title>Document</title>
    <style></style>
  </head>
  <body>
    <div>
      <h1>减少作用域链查找层级</h1>
      <p>下面案例优化了但是是有空间换了时间</p>
    </div>
    <script>
      var name = "xjq";
      function foo() {
        name = "xjq666"; //此处的name是属于全局的
        function bar() {
          var age = 38;
          console.log(age);
          console.log(name); //这里沿着bar向上查找,在沿着foo查找到全局作用域才找到name
        }
        bar();
      }
      foo();

      //优化
      var name = "xjq";
      function foo() {
        var name = "xjq666"; //此处的name是属于foo的
        function bar() {
          var age = 38;
          console.log(age);
          console.log(name); //这里沿着bar向上查找,在着foo查找到name
        }
        bar();
      }
      foo();
    </script>
  </body>
</html>
